package com.crk.shixiguanli.service.impl;

import com.crk.shixiguanli.dao.*;
import com.crk.shixiguanli.entity.*;
import com.crk.shixiguanli.service.PlanService;
import com.crk.shixiguanli.util.Page;
import com.crk.shixiguanli.util.ResponseResult;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.shiro.SecurityUtils;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * Description:
 *
 * @author:
 * Date: 2020-04-26
 * Time: 17:42
 */
@Service
public class PlanServiceImpl implements PlanService {
    @Autowired
    PlanDao planDao;
    @Autowired
    StudentDao studentDao;
    @Autowired
    PlanStudentDao planStudentDao;
    @Autowired
    UserDao userDao;
    @Autowired
    ProfessionalDao professionalDao;
    @Autowired
    TaskCompleteDao taskCompleteDao;
    @Autowired
    JobStudentDao jobStudentDao;


//    private final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @Override
    public ResponseResult queryPage(Page page) {
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        Integer count = planDao.countByCondition(userinfo.getId());
        page.setAllRow(count);
        List<Plan> planList  = planDao.selectByCondition(userinfo.getId(),page.getStart(), page.getSize());
        return getPlanVo(planList);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @Override
    public ResponseResult queryStudentDetail(String planName,String studentName,Page page) {
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        Integer count = planStudentDao.countByCondition(userinfo.getId(),planName,studentName);
        page.setAllRow(count);
        List<PlanStudentVo> planStudentVoList  = planStudentDao.selectByCondition(userinfo.getId(),planName,studentName,page.getStart(), page.getSize());
        return ResponseResult.success(planStudentVoList);
    }

    @Override
    public ResponseResult queryStudentComplete(Integer studentId) {
        TaskCompleteVo taskCompleteVo = taskCompleteDao.queryStudentComplete(studentId);
        return ResponseResult.success(taskCompleteVo);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public ResponseResult queryAll() {
        List<Plan> planList = planDao.selectAll();
        return getPlanVo(planList);
    }

    /**
     * 查询单个
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getOne(Integer id) {
        Plan plan = planDao.selectByPrimaryKey(id);
        return ResponseResult.success(plan);
    }

    @Override
    public ResponseResult getStudentComplete(Integer id) {
        PlanStudent planStudent = planStudentDao.selectByPrimaryKey(id);
        PlanStudentVo planStudentVo = new PlanStudentVo();
        planStudentVo.setId(planStudent.getId());
        planStudentVo.setEvaluate(planStudent.getEvaluate());
        planStudentVo.setScore(planStudent.getScore());
        Plan plan = planDao.selectByPrimaryKey(planStudent.getPlanId());
        planStudentVo.setPlanName(plan.getPlanName());
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        planStudentVo.setUserType(userinfo.getUserType());
        return ResponseResult.success(planStudentVo);
    }

    @Override
    public ResponseResult getMyPlan() {
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        PlanStudent planStudent = planStudentDao.selectByStudent(userinfo.getId());
        if (null == planStudent){
            return ResponseResult.success(null);
        }
        Plan plan = planDao.selectByPrimaryKey(planStudent.getPlanId());
        PlanVo planVo = new PlanVo();
        planVo.setId(plan.getId());
        planVo.setPlanName(plan.getPlanName());
        planVo.setStartTime(plan.getStartTime());
        planVo.setEndTime(plan.getEndTime());
        planVo.setUserName(userDao.selectByPrimaryKey(plan.getUpdater()).getUserName());
        planVo.setUpdateTime(plan.getUpdateTime());
        planVo.setPlanDesc(plan.getPlanDesc());
        planVo.setEvaluationCriterion(plan.getEvaluationCriterion());
        int compareTo = new Date().compareTo(plan.getEndTime());
        if(compareTo == 1){
            planVo.setStatusStr("已过期");
        }else{
            planVo.setStatusStr("进行中");
        }
        String joinPeople = plan.getJoinPeople();
        if (joinPeople.equals("-1")){
            planVo.setJoinPeopleStr("所有专业");
        }else{
            String[] split = joinPeople.split(",");
            List<String> stringBuffer = new ArrayList<>();
            for (String s : split) {
                String name = professionalDao.selectByCode(s);
                stringBuffer.add(name);
            }
            planVo.setJoinPeopleStr(String.join(",", stringBuffer));
        }

        planVo.setUserType(userinfo.getUserType());

        planVo.setHasJoin(true);

        planVo.setEvaluate(planStudent.getEvaluate());
        planVo.setScore(planStudent.getScore());

        return ResponseResult.success(planVo);
    }

    /**
     * 加入计划
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult joinPlan(Integer id) {
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        PlanStudent planStudent = new PlanStudent();
        planStudent.setStudentId(userinfo.getId());
        planStudent.setPlanId(id);
        planStudent.setStatus(1);
        int insert = planStudentDao.insert(planStudent);
        if (insert >0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }

    /**
     * 查询单个
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getOneShow(Integer id) {
        Plan plan = planDao.selectByPrimaryKey(id);
        PlanVo planVo = new PlanVo();
        planVo.setId(plan.getId());
        planVo.setPlanName(plan.getPlanName());
        planVo.setStartTime(plan.getStartTime());
        planVo.setEndTime(plan.getEndTime());
        planVo.setUserName(userDao.selectByPrimaryKey(plan.getUpdater()).getUserName());
        planVo.setUpdateTime(plan.getUpdateTime());
        planVo.setPlanDesc(plan.getPlanDesc());
        planVo.setEvaluationCriterion(plan.getEvaluationCriterion());
        int compareTo = new Date().compareTo(plan.getEndTime());
        if(compareTo == 1){
            planVo.setStatusStr("已过期");
        }else{
            planVo.setStatusStr("进行中");
        }
        String joinPeople = plan.getJoinPeople();
        if (joinPeople.equals("-1")){
            planVo.setJoinPeopleStr("所有专业");
        }else{
            String[] split = joinPeople.split(",");
            List<String> stringBuffer = new ArrayList<>();
            for (String s : split) {
                String name = professionalDao.selectByCode(s);
                stringBuffer.add(name);
            }
            planVo.setJoinPeopleStr(String.join(",", stringBuffer));
        }
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        planVo.setUserType(userinfo.getUserType());
        PlanStudent planStudent = planStudentDao.selectByStudent(userinfo.getId());
        if (planStudent !=null){
            planVo.setHasJoin(true);
        }else{
            planVo.setHasJoin(false);
        }
        return ResponseResult.success(planVo);
    }

    /**
     * 保存数据
     *
     * @param plan
     * @return
     */
    @Override
    public ResponseResult save(Plan plan) {
        if (plan.getId() == null){
            User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
            plan.setCreateTime(new Date());
            plan.setCreator(userinfo.getId());
            plan.setUpdateTime(new Date());
            plan.setUpdater(userinfo.getId());
            plan.setStatus(1);
            int insert = planDao.insert(plan);
            if (insert<=0){
                return ResponseResult.fail();
            }
            return ResponseResult.success();
        }else{
            User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
            plan.setUpdateTime(new Date());
            plan.setUpdater(userinfo.getId());
            int updateByPrimaryKey = planDao.updateByPrimaryKey(plan);
            if (updateByPrimaryKey<=0){
                return ResponseResult.fail();
            }
            return ResponseResult.success();
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        int i = planDao.deleteByPrimaryKey(id);
        if (i<=0){
            return ResponseResult.fail();
        }
        return ResponseResult.success();
    }

    /**
     * 查询计划列表-学生页面
     *
     * @return
     */
    @Override
    public ResponseResult queryPlanList() {
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        Student studentByIdCard = studentDao.getStudentByIdCard(userinfo.getIdCard());
        List<Plan> planList = planDao.selectByProfession(studentByIdCard.getProfessional());
        return getPlanVo(planList);
    }


    private ResponseResult getPlanVo(List<Plan> planList){
        List<PlanVo> result = new ArrayList<>();
        for (Plan plan : planList) {
            PlanVo planVo = new PlanVo();
            planVo.setId(plan.getId());
            planVo.setPlanName(plan.getPlanName());
            planVo.setStartTime(plan.getStartTime());
            planVo.setEndTime(plan.getEndTime());
            planVo.setUserName(userDao.selectByPrimaryKey(plan.getUpdater()).getUserName());
            planVo.setUpdateTime(plan.getUpdateTime());
            planVo.setPlanDesc(plan.getPlanDesc());
            planVo.setEvaluationCriterion(plan.getEvaluationCriterion());
            int compareTo = new Date().compareTo(plan.getEndTime());
            if(compareTo == 1){
                planVo.setStatusStr("已过期");
            }else{
                planVo.setStatusStr("进行中");
            }
            String joinPeople = plan.getJoinPeople();
            if (joinPeople.equals("-1")){
                planVo.setJoinPeopleStr("所有专业");
            }else{
                String[] split = joinPeople.split(",");
                List<String> stringBuffer = new ArrayList<>();
                for (String s : split) {
                    String name = professionalDao.selectByCode(s);
                    stringBuffer.add(name);
                }
                planVo.setJoinPeopleStr(String.join(",", stringBuffer));
            }
            result.add(planVo);
        }
        return ResponseResult.success(result);
    }

    @Override
    public ResponseResult evaluate(int id, String evaluate, String score) {
        planStudentDao.evaluate(id,evaluate,score);
        return ResponseResult.success();
    }

    /**
     * 统计数量
     *
     * @return
     */
    @Override
    public ResponseResult getCount() {
//        系统已录入的学生数量
        int studentCount = studentDao.getCount();
//        已注册的学生数量
        int studentUserCount =  userDao.getCount("student");
//        已注册的教师数
        int teacherUserCount =  userDao.getCount("teacher");
//        已注册的公司数
        int companyUserCount =  userDao.getCount("company");
//        已参加实习计划的学生数
        int planStudentCount = planStudentDao.getCount();
//        已参加实习岗位的学生数
        int jobStudentCount = jobStudentDao.getCount();
        HashMap<String,Object> count = new HashMap<>();
        count.put("studentUserCount",studentUserCount);
        count.put("teacherUserCount",teacherUserCount);
        count.put("companyUserCount",companyUserCount);
        count.put("studentCount",studentCount);
        count.put("planStudentCount",planStudentCount);
        count.put("jobStudentCount",jobStudentCount);
        return ResponseResult.success(count);
    }

    @Override
    public ResponseResult getStudentProfessionalMap() {
        HashMap<String,Object> data = new HashMap<>();
        List<HashMap<String,Object>> map = planDao.getStudentProfessionalMap();
        List<String> legend = new ArrayList<>();
        for (HashMap<String, Object> stringObjectHashMap : map) {
            legend.add(stringObjectHashMap.get("name").toString());
        }
        data.put("legend",legend);
        data.put("result",map);
        return ResponseResult.success(data);
    }

    @Override
    public ResponseResult getStudentPlanMap() {
        HashMap<String,Object> data = new HashMap<>();
        List<HashMap<String,Object>> map = planDao.getStudentPlanMap();
        List<String> legend = new ArrayList<>();
        for (HashMap<String, Object> stringObjectHashMap : map) {
            legend.add(stringObjectHashMap.get("name").toString());
        }
        data.put("legend",legend);
        data.put("result",map);
        return ResponseResult.success(data);
    }

    @Override
    public ResponseResult getStudentJobMap() {
        HashMap<String,Object> data = new HashMap<>();
        List<HashMap<String,Object>> map = planDao.getStudentJobMap();
        List<String> legend = new ArrayList<>();
        for (HashMap<String, Object> stringObjectHashMap : map) {
            legend.add(stringObjectHashMap.get("name").toString());
        }
        data.put("legend",legend);
        data.put("result",map);
        return ResponseResult.success(data);
    }
}
